package com.volunteer.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.common.R;
import com.volunteer.entity.Evaluation;
import com.volunteer.service.EvaluationService;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/admin/evaluation")
@AllArgsConstructor
public class AdminEvaluationController {

    private final EvaluationService evaluationService;

    /**
     * 获取评价列表（分页）
     */
    @GetMapping("/list")
    public R<Page<Evaluation>> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status) {

        LambdaQueryWrapper<Evaluation> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果有关键字，则搜索活动ID或评价内容
        if (StringUtils.hasText(keyword)) {
            queryWrapper.like(Evaluation::getActivityId, keyword)
                    .or()
                    .like(Evaluation::getContent, keyword);
        }
        
        // 如果指定了状态，则按状态筛选
        if (status != null) {
            queryWrapper.eq(Evaluation::getStatus, status);
        }
        
        // 按创建时间倒序排序
        queryWrapper.orderByDesc(Evaluation::getCreateTime);
        
        Page<Evaluation> page = evaluationService.page(
            new Page<>(pageNum, pageSize), 
            queryWrapper
        );
        
        return R.ok(page);
    }

    /**
     * 审核通过评价
     */
    @PostMapping("/approve/{id}")
    public R<String> approve(@PathVariable Long id) {
        Evaluation evaluation = new Evaluation();
        evaluation.setId(id);
        evaluation.setStatus(1); // 1-已通过
        evaluation.setAuditTime(LocalDateTime.now());
        boolean success = evaluationService.updateById(evaluation);
        return success ? R.ok("审核通过成功") : R.fail("审核失败");
    }

    /**
     * 拒绝评价
     */
    @PostMapping("/reject/{id}")
    public R<String> reject(@PathVariable Long id, @RequestParam(required = false) String remark) {
        Evaluation evaluation = new Evaluation();
        evaluation.setId(id);
        evaluation.setStatus(2); // 2-已拒绝
        evaluation.setAuditTime(LocalDateTime.now());
        if (StringUtils.hasText(remark)) {
            evaluation.setAuditRemark(remark);
        }
        boolean success = evaluationService.updateById(evaluation);
        return success ? R.ok("已拒绝该评价") : R.fail("操作失败");
    }
    
    /**
     * 删除评价
     */
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean success = evaluationService.removeById(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }
    
    /**
     * 根据ID获取评价详情
     */
    @GetMapping("/get/{id}")
    public R<Evaluation> getById(@PathVariable Long id) {
        Evaluation evaluation = evaluationService.getById(id);
        return evaluation != null ? R.ok(evaluation) : R.fail("未找到该评价");
    }
}
